<?php
/**
 * Created by PhpStorm.
 * User: jksen
 * Date: 2018/1/22
 * Time: 上午10:04
 */

namespace app\admin\controller\v1;

use app\admin\controller\Fun;
use app\api\controller\Errorcode;
use app\admin\service\SAdminUser;
use app\common\Cache\CAdminUser;

/**
 * @title 后台 - 用户类 - V1
 * @description 接口说明
 *
 * @header name:copyright require:1 default:v1 desc:版本号(全局)
 */
class AdminUser
{
    private $_data;

    public function __construct($_data)
    {
        $this->_data = $_data;
    }

    /**
     * @title 后台用户登录
     * @description
     * @author jikesen
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:AdminUser desc:接口组(全局)
     *
     * @param name:command type:string require:1 default: other:loginByuid desc:获取上传资质
     * @param name:uid type:string require:1 default: other:10086 desc:用户id
     * @param name:pwd type:string require:1 default: other:123456 desc:用户密码
     *
     * @return data:
     */
    public function loginByuid()
    {
        if (empty(trim($this->_data['uid'])) || empty(trim($this->_data['pwd']))) {
            return Fun::error('999993', Errorcode::$code['999993']);
        }

        #1 校验
        $user = new SAdminUser();

        $res = $user->checkUser(trim($this->_data['uid']), trim($this->_data['pwd']));

        if ($res['code'] != '000000') {
            return Fun::error($res['code'], $res['msg']);
        }

        #2 缓存
        CAdminUser::cacheUser($res['data']);

        #3 返回 accid token
        return Fun::success([
            'uid' => $res['data']['uid'],
            'acc_id' => $res['data']['acc_id'],
            'access_token' => $res['data']['access_token'],
        ]);
    }
}